/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.odbs.service;

import com.etop.odbs.beans.Document;
import com.etop.odbs.beans.OdbsException;
import com.etop.odbs.beans.Report;
import com.etop.odbs.beans.User;
import com.etop.odbs.dao.ChackDao;
import com.etop.odbs.dao.CollectionDao;
import com.etop.odbs.dao.CommentDao;
import com.etop.odbs.dao.DocumentDao;
import com.etop.odbs.dao.DownloadDao;
import com.etop.odbs.dao.ReportDao;
import com.etop.odbs.dao.UserDao;
import com.etop.odbs.utils.MD5;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author cjh
 */
public class UserService {

    private UserDao userDao;
    private DocumentDao documentDao;
    private ChackDao chackDao;
    private CollectionDao collectionDao;
    private CommentDao commentDao;
    private DownloadDao downloadDao;
    private ReportDao reportDao;

    public CommentDao getCommentDao() {
        return commentDao;
    }

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    public DownloadDao getDownloadDao() {
        return downloadDao;
    }

    public void setDownloadDao(DownloadDao downloadDao) {
        this.downloadDao = downloadDao;
    }

    public ReportDao getReportDao() {
        return reportDao;
    }

    public void setReportDao(ReportDao reportDao) {
        this.reportDao = reportDao;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public DocumentDao getDocumentDao() {
        return documentDao;
    }

    public void setDocumentDao(DocumentDao documentDao) {
        this.documentDao = documentDao;
    }

    public ChackDao getChackDao() {
        return chackDao;
    }

    public void setChackDao(ChackDao chackDao) {
        this.chackDao = chackDao;
    }

    public CollectionDao getCollectionDao() {
        return collectionDao;
    }

    public void setCollectionDao(CollectionDao collectionDao) {
        this.collectionDao = collectionDao;
    }

    /* ************************************************************************
     *
     * 下面开始写UserService的代码
     *
     *************************************************************************/
    /**
     * 处理用户登录
     *
     * @param key 用户登录的账户，可以是电话号码或者邮箱
     * @param password 用户密码
     * @return 登陆成功后用户的实例
     */
    public User load(String key, String password) {
        password = new MD5().getkeyBeanofStr(password);
        System.out.println(password);
        User u = userDao.load(key);
        if (u == null) {
            throw new OdbsException("用户名不存在");
        } else if (!password.equals(u.getPassword())) {
            throw new OdbsException("用户密码错误");
        }
        return u;
    }

    /**
     * 处理用户的上传
     *
     * @param doc
     * @param uploadFile
     */
    public void addDocument(Document doc, MultipartFile uploadFile) {
        try {
            uploadFile.transferTo(new File(doc.getPath()));
        } catch (IOException | IllegalStateException ex) {
            Logger.getLogger(UserService.class.getName()).log(Level.SEVERE, null, ex);
        }
        documentDao.addDocument(doc);
        
        Document d = new Document();
        d.setId(10);
        Report r = new Report();
        r.setDocument(d);
        r.setReason("adfsf");
        r.setUser(doc.getUser());
        reportDao.add(r);
    }

    
    //添加收藏记录
    public void addCollection(Collection c){
        collectionDao.add(c);
    }
    //读取收藏记录
    public List loadCollection(int id){
        return collectionDao.load(id);
    }
    //删除收藏记录
    public void deleteCollection(int u_id,int d_id){
        collectionDao.delete(u_id, d_id);
    }
    
}
